package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.callback.ICaseView;
import com.tj.jinzao.fragment.CompleteInfoStep1Fragment;
import com.tj.jinzao.fragment.CompleteInfoStep2Fragment;
import com.tj.jinzao.fragment.CompleteInfoStep3Fragment;
import com.tj.jinzao.fragment.CompleteInfoStep4Fragment;
import com.tj.jinzao.fragment.CompleteInfoStep5Fragment;
import com.tj.jinzao.fragment.CompleteInfoStep6Fragment;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.presenter.CompleteInfoPresenter;

import java.util.HashMap;
import java.util.List;

/**
 * 完善信息界面
 * Created by wjy on 2019/1/6.
 */
public class CompleteInfoActivity extends BaseActivity implements ICaseView<List<Target>> {

    private int mStep = 1;
    private HashMap<String, Object> mData;

    private Fragment mInfoStep1Fragment, mInfoStep2Fragment, mInfoStep3Fragment, mInfoStep4Fragment, mInfoStep5Fragment, mInfoStep6Fragment, mInfoStep7Fragment;

    private Fragment mCurrentFragment;

    private CompleteInfoPresenter mPresenter;

    @Override
    protected void setupView() {
        super.setupView();
        mData = new HashMap();
        mPresenter = new CompleteInfoPresenter(this);
        switchFragment();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    public void setData(String key, Object data) {
        mData.put(key, data);
    }

    public Object getData(String key) {
        return mData.get(key);
    }

    @Override
    protected void clickLeftButton() {
        if(getIntent()!=null&&(getIntent().getIntExtra("mType",1)==2||getIntent().getIntExtra("mType",1)==3||getIntent().getIntExtra("mType",1)==4)){
            finish();
        }else {
            if (mStep > 1) {
                mStep--;
                switchFragment();
            }

        }
    }

    @Override
    protected int getLeftButtonResId() {
        if(getIntent()!=null&&(getIntent().getIntExtra("mType",1)==2||getIntent().getIntExtra("mType",1)==3||getIntent().getIntExtra("mType",1)==4)){
            return R.mipmap.ic_back;
        }
        return super.getLeftButtonResId();
    }


    @Override
    public int getLayoutResource() {
        return R.layout.base_activity_fragment_layout;
    }

    public void next() {
        mStep++;
        Log.w("用户信息接口","wwww"+mStep);
        if (mStep > 7) {
            mPresenter.updateInfo(mData);
        } else switchFragment();
    }

    public void switchFragment() {
        //        if (mIndex == index) return;
        if(getIntent()!=null&&getIntent().getIntExtra("mStep",1)>1&&mStep==1){
            mStep= getIntent().getIntExtra("mStep",1);
        }
        setTitleText("完善资料" + mStep + "/7");
        if(getIntent()!=null&&getIntent().getIntExtra("mType",1)==2){
            setTitleText("个人目标");
        }else if(getIntent()!=null&&getIntent().getIntExtra("mType",1)==3){
            setTitleText("身体状况");
        }else if(getIntent()!=null&&getIntent().getIntExtra("mType",1)==4){
            setTitleText("给用户打标签");
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurrentFragment != null) transaction.hide(mCurrentFragment);
        switch (mStep) {
            case 1:
                if (mInfoStep1Fragment == null) {
                    mInfoStep1Fragment = new CompleteInfoStep1Fragment();
                }
                mCurrentFragment = mInfoStep1Fragment;
                break;
            case 2:
                if (mInfoStep2Fragment == null) {
                    mInfoStep2Fragment = new CompleteInfoStep2Fragment();
                }
                mCurrentFragment = mInfoStep2Fragment;
                break;
            case 3:
                if (mInfoStep3Fragment == null) {
                    mInfoStep3Fragment = new CompleteInfoStep3Fragment();
                }
                mCurrentFragment = mInfoStep3Fragment;
                break;
            case 4:
                if (mInfoStep4Fragment == null) {
                    mInfoStep4Fragment = new CompleteInfoStep4Fragment();
                }
                mCurrentFragment = mInfoStep4Fragment;
                break;
            case 5:
                if (mInfoStep5Fragment == null) {
                    mInfoStep5Fragment = new CompleteInfoStep5Fragment();
                }
                mCurrentFragment = mInfoStep5Fragment;
                break;
            case 6:
                if (mInfoStep6Fragment == null) {
                    mInfoStep6Fragment = CompleteInfoStep6Fragment.newInstance(0);
                }
                mCurrentFragment = mInfoStep6Fragment;
                break;
            case 7:
                if (mInfoStep7Fragment == null) {
                    mInfoStep7Fragment = CompleteInfoStep6Fragment.newInstance(1);
                }
                mCurrentFragment = mInfoStep7Fragment;
                break;
        }
        try {
            if (mCurrentFragment != null) {
                if (!mCurrentFragment.isAdded()) {
                    transaction.add(R.id.fragment_container, mCurrentFragment);
                }
                transaction.show(mCurrentFragment).commitAllowingStateLoss();
            }
        } catch (Exception e) {
        }
    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<Target> targets) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }
}
